#!/usr/bin/perl
#mdm_upgradesubmit.cgi

require "./mdmjobs-lib.pl";
&header($text{'res_upgrade_detail'}, "", "index", 0, 1);
&foreign_require("upgrade","upgrade-lib.pl");

local $sv_mount_dev = &foreign_call("upgrade","get_sysvol_mount_dev");

if(($sv_mount_dev eq "") || ($sv_mount_dev =~ /ram/)){
	&error($text{'naslog_msg_sv_devram'});
}

if ($ENV{REQUEST_METHOD} eq "POST"){ 
	&ReadParseMime(); 
}else{
	&ReadParse(); 
	$no_upload = 1; 
}
&error_setup($text{'install_err'});
$| = 1;
if ($no_upload) {
	&error($text{'install_eupload'});
}


&remote_error_setup(\&inst_error);

local @in_members=split(/,/,$in{'listinput'});
local $grpinfo=&foreign_call("mdmgroups","get_groupinfo");
local %grpinfo=%$grpinfo;
local @group = split (/,/,$grpinfo{'[group]'});
local $own_upgrade=0;

local @devices;
foreach(@in_members){
	if (/\[This server\]/){
		$own_upgrade=1;
		next;
	}elsif (/^@/){
		local $grpname=$_;
		$grpname=~ s/@//;
		local $devs=$grpinfo{"$grpname"};
		local @devs=split(/,/,$devs);
		foreach $s (@devs){
			if ($s=~/\[This server\]/){
				$own_upgrade=1;
				next;
			}						
			push (@devices,$s);
		}
	}elsif(/^\[/){
		push (@devices,$_);
	}	
}
local %single;
local @servers;
foreach $serv (@devices){
	if (!$single->{"$serv"}++){
		push @servers,$serv;
	}
}

open(UPLOAD, ">$config{'uploadfile'}");
print UPLOAD $in{'inputfile'};
close(UPLOAD);

open(UPLOAD, ">$config{'uploadfiletmp'}");
print UPLOAD $in{'inputfile'};
close(UPLOAD);

my $err_msg = &foreign_call("upgrade","dcrypt_uploadfile");
if($err_msg){	
	&error($err_msg);
}

$err_msg = &foreign_call("upgrade","extract_uploadfile");
if($err_msg){
	&error($err_msg);
}

&ez_system('rm', '-rf', $config{'upgradedir'});
&ez_system('rm', '-f', $config{'uploadfilecpt'});
&ez_system('rm', '-f', $config{'uploadfiletar'});
&ez_system('cp', '-f', $config{'uploadfiletmp'}, $config{'uploadfile'});
local @devlist1=&foreign_call("mdmsearch","list_controlledservs");

@devices = sort { $a cmp $b } @servers;
$p=0;
foreach $s (@devices){
	if ($s=~/\[This server\]/){
		next;
	}
	
	local ($rh = "READ$p", $wh = "WRITE$p");
	pipe($rh, $wh);
	select($wh); $| = 1; select(STDOUT);
	
	local $hostid = $s;
	$hostid =~ s/\[//;
	$hostid =~ s/\].*//;
	local $hostip="0.0.0.0";
	local $host;
	foreach $host (@devlist1){
		if ($host->{"id"} eq $hostid){
			$hostip=$host->{"ip"};
			last;
		}
	}

	if (!fork()) {
		close($rh);
		local $rst;
		local @rst;
		&remote_foreign_require($hostid, "upgrade","upgrade-lib.pl");	
		local $sysvol_res = &remote_foreign_call($hostid,"upgrade","get_sysvol_mount_dev");
		if(($sysvol_res eq "") || ($sysvol_res =~ /ram/)){
			push(@rst,$text{'naslog_msg_sv_devram'});
			print $wh &serialise_variable(\@rst);
			close($wh);
			exit;
		}		
		$rst = &remote_write($hostid,$config{'uploadfiletmp'},$config{'uploadfile'},$hostip);
		#push(@rst,$rst);
		$rst = &remote_foreign_call($hostid, "upgrade", "mdm_update");
		push(@rst,$rst);
		print $wh &serialise_variable(\@rst);
		close($wh);
		exit;
	}
	
	close ($wh);
	$p++;	
}

if($own_upgrade == 1){
	local $rst= &foreign_call("upgrade","mdm_update");
	push @data,"This Server return is: $rst";
	print "<p>This Server return is: $rst\n";
}

local @data;
local $logoper = "Upgrade devices";
$p = 0;

foreach $s (@devices) {
	if (/\[This server\]/){
		next;
	}
	local $rh = "READ$p";
	local $line = <$rh>;
	close($rh);
	
	local $rv = &unserialise_variable($line);
	@rv=@$rv;
	if (! @rv){
		push @data,"$s return is: $text{'noanswer'}";
		print "<p>$s return is: $text{'noanswer'}";
	}else{
		local $line=join("; ",@rv);
		push @data,"$s return is: $line";
		print "<p>$s return is: $line\n";
	}
	
	$p++;	
}

&ez_system('rm', '-rf', $config{'uploadfiletmp'});

print "<p>$text{'msg_toreboot1'}\n";
print "<a href='../mdmjobs/reboot_setting.cgi'>\n";
print "$text{'reboot_detail'}</a>\n";
print "$text{'msg_toreboot2'}</td></tr>\n";

&foreign_call("mdmhistory","mdm_log",$logoper,\@data);	
&remote_finished();	
&footer("./index.cgi", $text{'index_title'});		